#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using System.Web;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class UserControlManageCheckIn : UserControl
    {
        User m_current_user;
        CheckInService m_BookingtService;
        Keys m_currentKey;
        MainForm m_mainForm;
        SeaPortService m_SeaPortService;
        TravelAgentService m_travelAgentService;
        TripService m_tripService;
        //EditMode m_editmode;

        public UserControlManageCheckIn(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            this.Name = name;
            m_current_user = currentUser;
            m_BookingtService = (CheckInService)ServiceFactory.Instance().GetService(ServiceFactory.BOOKING_SERVICE);
            m_SeaPortService = (SeaPortService)ServiceFactory.Instance().GetService(ServiceFactory.SEAPORT_SERVICE);
            m_tripService = (TripService)ServiceFactory.Instance().GetService(ServiceFactory.TRIP_SERVICE);
            m_travelAgentService = (TravelAgentService)ServiceFactory.Instance().GetService(ServiceFactory.TRAVEL_AGENT_SERVICE);
            m_mainForm = mainForm;
            setPermission();
            //textBoxSearch.SelectAll();

            LoadAllBooking();
            initToolBarButtonClick();
            enableButton(false);

            toolBarButtonClear.Visible = false;
            toolBarButtonDelete.Visible = false;
            timerUpdate.Start();
        }


        private void initToolBarButtonClick()
        {
            toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            toolBarButtonView.Click += new EventHandler(toolBarButtonView_Click);
            toolBarButtonPrintItinerary.Click += new EventHandler(toolBarButtonPrintItinerary_Click);
        }

        void toolBarButtonPrintItinerary_Click(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            CheckIn user = (CheckIn)listViewRecords.SelectedItem.Tag;
            PrintItineraryForm pif = new PrintItineraryForm(user.m_bookingID, m_current_user);
            pif.ShowDialog();
        }

        void toolBarButtonView_Click(object sender, EventArgs e)
        {
            loadCheckInForm(EditMode.Load);
        }

        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            CheckIn user = (CheckIn)listViewRecords.SelectedItem.Tag;
            if (user.m_trip.m_departureStatus == DepartureStatus.Close)
                loadCheckInForm(EditMode.Load);
            else
                loadCheckInForm(EditMode.Edit);
            //loadCheckInForm(EditMode.Edit);
        }

        void enableButton(bool enable)
        {
            toolBarButtonDelete.Enabled = enable && m_current_user.RoleDetails(this.Name).DeleteFlag;
            toolBarButtonEdit.Enabled = enable && m_current_user.RoleDetails(this.Name).UpdateFlag;
            toolBarButtonView.Enabled = enable && m_current_user.RoleDetails(this.Name).ViewFlag;
            toolBarButtonPrintItinerary.Enabled = enable && m_current_user.RoleDetails(this.Name).ViewFlag;
        }

        private void setPermission()
        {
            toolBarButtonDelete.Enabled = m_current_user.RoleDetails(this.Name).DeleteFlag;
            toolBarButtonEdit.Enabled = m_current_user.RoleDetails(this.Name).UpdateFlag;
            toolBarButtonClear.Enabled = m_current_user.RoleDetails(this.Name).NewFlag;
            toolBarButtonView.Enabled = m_current_user.RoleDetails(this.Name).ViewFlag;
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            buttonFind_Click(sender, e);
            //LoadAllBooking();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            MessageBox.Show("Are you sure to delete this record?", "Confirmation", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, new EventHandler(DeleteConfirmation));
        }

        void DeleteConfirmation(object sender, EventArgs e)
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    CheckIn user = (CheckIn)listViewRecords.SelectedItem.Tag;
                    if (CheckPassengerBookStatus(user))
                    {
                        MessageBox.Show("Book status has been confirmed, could not delete.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    }
                    else
                    {
                        if (user.m_bookingID > 0)
                        {
                            m_BookingtService.DeleteCheckIn(user);
                            MessageBox.Show("Record has been deleted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                            // clearForm();
                            //LoadAllBooking();
                            //setEditMode(EditMode.Deleted);
                            listViewRecords.Items.RemoveAt(listViewRecords.SelectedItem.Index);
                        }
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private bool CheckPassengerBookStatus(CheckIn user)
        {
            bool confirm = false;
            foreach (Passenger pss in user.m_passengerList)
            {
                if (pss.m_passengerStatus == PassengerStatus.Boarding)
                {
                    confirm = true;
                    break;
                }
            }
            return confirm;
        }

        void toolBarButtonClear_Click(object sender, EventArgs e)
        {
            CheckInForm frm = new CheckInForm(this.Name, new CheckIn(), m_current_user, m_mainForm, this, EditMode.Create);
            frm.ShowDialog();

        }

        public void LoadAllBooking()
        {
            buttonFind_Click(null, null);
            //listViewRecords.Items.Clear();
            //IList users = m_current_user.UserType == UserType.Administrators ? m_BookingtService.LoadAllBooking() : m_BookingtService.LoadAllBookingByTravelAgent(m_current_user.TravelAgent.TravelAgentID);
            //foreach (Booking booking in users)
            //{
            //    //if (booking.m_bookingStatus == BookingStatus.ReConfirmed)
            //    //{
            //    //    booking.BookingHistory = m_BookingtService.GetBookingHistoryByBookingID(booking.m_bookingID);
            //    //}
            //    booking.m_trip = m_tripService.GetTripByID(booking.m_trip.m_tripID);
            //    booking.m_trip.m_seaPort = m_SeaPortService.GetSeaPortByID(booking.m_trip.m_seaPort.m_seaPortID);
            //    booking.m_travelAgent = m_travelAgentService.GetTravelAgentByID(booking.m_travelAgent.TravelAgentID);
            //    ListViewItem item = listViewRecords.Items.Add(booking.m_bookingCode);
            //    if (booking.m_bookingStatus == BookingStatus.ReConfirmed)
            //        item.ForeColor = Color.Red;
            //    item.SubItems.Add(booking.m_bookingStatus.ToString());
            //    item.SubItems.Add(booking.m_travelAgent.Name);
            //    item.SubItems.Add(booking.m_groupName);
            //    item.SubItems.Add(booking.m_bookingDate.ToString("dd-MMM-yyyy"));
            //    item.SubItems.Add(booking.m_bookNumber.ToString());
            //    item.SubItems.Add(booking.m_ticketType.ToString());
            //    item.SubItems.Add(booking.m_trip.m_seaPort.m_seaPortName);
            //    item.SubItems.Add(booking.m_trip.m_tripCode);
            //    item.SubItems.Add(booking.m_trip.m_departureDate.ToString("dd-MMM-yyyy"));
            //    item.SubItems.Add(booking.m_trip.m_departureTime.ToString("HH:mm"));
            //    item.Tag = booking;
            //}
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            listViewRecords.Items.Clear();
            //IList users = m_current_user.UserType == UserType.Administrators ? 
            //    m_BookingtService.SearchCheckInByCodeNameDepartureDate(textBoxSearch.Text, dateTimePickerSearch.Value) 
            //    : m_BookingtService.SearchCheckInByCodeNameAndTravelAgent(textBoxSearch.Text, m_current_user.TravelAgent.TravelAgentID, dateTimePickerSearch.Value);
            IList tripList = m_BookingtService.SearchCheckInByCodeNameTripDepartureDate("", dateTimePickerSearch.Value);
            foreach (CheckIn checkin in tripList)
            {
                //checkin.m_trip = m_tripService.GetTripByID(checkin.m_trip.m_tripID);
                //checkin.m_trip.m_seaPort = m_SeaPortService.GetSeaPortByID(checkin.m_trip.m_seaPort.m_seaPortID);
                //checkin.m_travelAgent = m_travelAgentService.GetTravelAgentByID(checkin.m_travelAgent.TravelAgentID);
                //ListViewItem item = listViewRecords.Items.Add(checkin.m_bookingCode);
                //if (checkin.m_bookingStatus == CheckInStatus.ReCeckIn)
                //    item.ForeColor = Color.Red;
                //item.SubItems.Add(checkin.m_bookingStatus.ToString());
                //item.SubItems.Add(checkin.m_travelAgent.Name);
                //item.SubItems.Add(checkin.m_groupName);
                //item.SubItems.Add(checkin.m_bookingDate.ToString("dd-MMM-yyyy"));
                //item.SubItems.Add(checkin.m_bookNumber.ToString());
                //item.SubItems.Add(checkin.m_ticketType.ToString());
                //item.SubItems.Add(checkin.m_trip.m_seaPort.m_seaPortName);
                //item.SubItems.Add(checkin.m_trip.m_tripCode);
                //item.SubItems.Add(checkin.m_trip.m_departureDate.ToString("dd-MMM-yyyy"));
                //item.SubItems.Add(checkin.m_trip.m_departureTime.ToString("HH:mm"));
                //item.Tag = checkin;

                checkin.m_trip = m_tripService.GetTripByID(checkin.m_trip.m_tripID);
                checkin.m_trip.m_seaPort = m_SeaPortService.GetSeaPortByID(checkin.m_trip.m_seaPort.m_seaPortID);
                checkin.m_travelAgent = m_travelAgentService.GetTravelAgentByID(checkin.m_travelAgent.TravelAgentID);
                ListViewItem item = listViewRecords.Items.Add(checkin.m_trip.m_tripCode);
                item.SubItems.Add(checkin.m_trip.m_departureDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(checkin.m_trip.m_departureTime.ToString("HH:mm"));
                item.SubItems.Add(checkin.m_trip.m_seaPort.m_seaPortName);
                item.SubItems.Add(checkin.m_trip.m_departureStatus.ToString());
                item.Tag = checkin;
            }
            if (tripList.Count > 1)
            {
                listViewRecords.Focus();
                listViewRecords.SelectedIndex = 0;
            }
            else if (tripList.Count == 0) 
            {
                ListViewItem item = listViewRecords.Items.Add("Not Found.");
               // textBoxSearch.SelectAll(); 
            }
        }

        private void listViewRecords_DoubleClick(object sender, EventArgs e)
        {
            //if (toolBarButtonEdit.Enabled)
            //    loadUserForm(EditMode.Edit);
            //else
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            CheckIn user = (CheckIn)listViewRecords.SelectedItem.Tag;
            if (user.m_trip.m_departureStatus == DepartureStatus.Close)
                loadCheckInForm(EditMode.Load);
            else
            {
                if (m_current_user.RoleDetails(this.Name).UpdateFlag || m_current_user.RoleDetails(this.Name).NewFlag)
                    loadCheckInForm(EditMode.Edit);
                else
                    loadCheckInForm(EditMode.Load);
            }
        }

        private void loadCheckInForm( EditMode editmode )
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            CheckIn user = (CheckIn)listViewRecords.SelectedItem.Tag;
            CheckInForm frm = new CheckInForm(this.Name, user, m_current_user, m_mainForm, this, editmode);
            frm.ShowDialog();
        }

        private void textBox_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            m_currentKey = objArgs.KeyData;
        }


        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (m_currentKey == Keys.Enter)
            {
                buttonFind_Click(null, null);
            }
        }

        private void listViewRecords_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (objArgs.KeyCode == Keys.Enter)
            {
                listViewRecords_DoubleClick(objSender, null);
            }
        }

        private void textBoxSearch_GotFocus(object sender, EventArgs e)
        {
            //textBoxSearch.SelectAll();
        }

        private void listViewRecords_SelectedIndexChanged(object sender, EventArgs e)
        {
            enableButton(listViewRecords.SelectedItem != null);            
            if (listViewRecords.SelectedItem != null)
                enableButton(listViewRecords.SelectedItem.Tag != null);
        }

        private void timerUpdate_Tick(object sender, EventArgs e)
        {
            int selectedIndex = listViewRecords.SelectedIndex;
            toolBarButtonRefresh_Click(sender, e);
            if (listViewRecords.Items.Count <= selectedIndex + 1)
            {
                listViewRecords.Items[selectedIndex].Selected = true;
            }
        }

        private void UserControlManageCheckIn_Load(object sender, EventArgs e)
        {
            listViewRecords.Select();
            if (listViewRecords.Items.Count > 0)
                listViewRecords.Items[0].Selected = true;
        }

        private void listViewRecords_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                listViewRecords_DoubleClick(sender, e);
            }
        }

    }
}